
package bankClient;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import bankservice.endpoint.BankInterface;

/**
 *
 * This simulates a client 
 * 
 * @author g_ogha
 */

public class BankClient
{

  public static void main(String args[]){
	  
	  	 URL url;

	     try{
	    	 
	    	 String accountNumber;
	         String branchCode;
	         
	         String destinationAccountNumber;
	         
	         boolean loopKey;
	         
	         String operationCode;
	         int operation;

	         String  amount;
	         
	         float depositeAmount;
	         float withdrawAmount;
	         float transferAmount;
	         float accountBalance;
	        			
	         System.out.println("____________________________");
	         System.out.println();
	         System.out.println("   Welcome to ATM Branch    ");
	         System.out.println("____________________________");
	         System.out.println();

	         InputStreamReader is = new InputStreamReader(System.in);
	         BufferedReader br = new BufferedReader(is);

	         System.out.println("Please enter your account number:");

	         accountNumber = br.readLine();
	        
	        // Extracting Branch Code from Account Number
	                 		
	         branchCode = accountNumber.substring(0,2);
	         
	         url = new URL("http://localhost:9876/endpoint?wsdl");
	         
	         if (branchCode.equals("01")){
	        	        
	        	 url = new URL("http://localhost:9876/endpoint?wsdl");	        	 
	         } else if (branchCode.equals("02")){
	        	 
	        	 url = new URL("http://localhost:9877/endpoint?wsdl");
	         }
				
		     QName qname = new QName("http://endpoint.bankservice/", "BankImplService");

	         Service service = Service.create(url, qname);
		     BankInterface eif = service.getPort(BankInterface.class);
		          
	         System.out.println("ATM Connected to main branch" );
	         System.out.println();
	        
	         loopKey = true;
	        
	         while(loopKey){
	        	
	        	 System.out.println("For requesting each operation enter the relevant number" );

	        	 System.out.println();
	        	 System.out.println("1 ==> Deposit" );
	        	 System.out.println();
	        	 System.out.println("2 ==> Withdraw" );
	        	 System.out.println();
	        	 System.out.println("3 ==> Balance" );
	        	 System.out.println();
	        	 System.out.println("4 ==> Transfer" );
	        	 System.out.println();
	        	 System.out.println("5 ==> Exit" );
	        	 System.out.println();
	        	
	        	 operationCode =  br.readLine();
	        	 operation = Integer.parseInt(operationCode);
	        	
	        	 switch (operation){
	        	
				case 1: System.out.println("Please enter the amount:");
	    				
	    				amount =  br.readLine();
	    		        depositeAmount = Integer.parseInt(amount);
	    				    		        
	    		        //ServerImpl.deposite(accountNumber, depositeAmount);
	    		        eif.deposite(accountNumber, depositeAmount);
	    		        
						break;
						
				case 2: System.out.println("Please enter the amount:");
				
						amount =  br.readLine();
						withdrawAmount = Integer.parseInt(amount);
															
						//ServerImpl.withdraw(accountNumber, withdrawAmount);
						eif.withdraw(accountNumber, withdrawAmount);
				
						break;
						
				case 3:	System.out.println("This is the balance of your account");
						
						//System.out.println(ServerImpl.getBalance(accountNumber));
						System.out.println(eif.getBalance(accountNumber));
						
						break;
						
				case 4:	System.out.println("Which account number you would like to transfer money:");
				
						destinationAccountNumber =  br.readLine(); 
								
						System.out.println("Please enter the amount of money which you would like to transfer:");
						
						amount =  br.readLine();
						transferAmount = Integer.parseInt(amount);
						
						if (transferAmount <= eif.getBalance(accountNumber)){
											
							accountBalance = eif.transfer(accountNumber, destinationAccountNumber, transferAmount);
							
							if (accountBalance == -1){
						
								System.out.println("The account number which you have entered is not valid! Please Try Again!" );
							}
							else{
								
								System.out.println("Money has been transfered! Your new account balance is: " + accountBalance );
							}
						}
						else{
																			
							System.out.println("Your current balance is less than the transfer amount!Please Try Again!");						
						}
											
						break;
						
				case 5:	System.out.println("Good bye");
				
						loopKey = false;				
						break; 				
				}
	        		        	
	        }// end of while
		  } 
	     catch (Exception e) {
	        System.out.println("ERROR : " + e) ;
		     e.printStackTrace(System.out);
		  } 
	  } //end main

} // end class
